import axios from "axios"
import {baseURL} from "./config"
import store from "../store/index"
const service=axios.create({
    baseURL,
    timeout:10000 //请求时间不超过5s
})

//请求拦截
service.interceptors.request.use(config=>{
    let token=store.state.token
    if(token){
        config.headers.Authorization=`Bearer ${token}`
    }
    // console.log(config)
    return config
    
},err=>{
    return Promise.reject(err)
})

//响应拦截
service.interceptors.response.use(res=>{
    //刷新token
    let refreshToken=res.headers.authorization || res.headers.Authorization
    if(refreshToken){
        store.commit("savetoken",refreshToken)
    }
    return res
},err=>{
    // let code=err.response.status
    // if(code==401){

    // }

    return Promise.reject(err)
})

export default service